package org.example.leetCode;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Vader
 * @date 2021/9/28
 **/
public class PathSum {
    int size;

    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return 0;
        }
        Sum(root, targetSum);
        pathSum(root.right, targetSum);
        pathSum(root.left, targetSum);
        return size;
    }

    private void Sum(TreeNode root, int targetSum) {
        if (root == null) {
            return;
        }
        targetSum -= root.val;
        if (targetSum == 0) {
            size++;
        }
        Sum(root.left, targetSum);
        Sum(root.right, targetSum);
    }


}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}